home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / PET / S-Super PET / (s)tz.d64 / FORMATING.ASM < prev    next >
Assembly Source File  |  2009-01-18  |  4KB  |  114 lines

  1. opt nolist              ;FORMATING   routine
  2.  
  3.         xdef formating,format_integer
  4.         xref address,precision, buffer, buffer_2,digits_after_point
  5.         xref fixed_format,field_width,spaces,digits_before_point
  6.         xref suffixst_,fdiv_
  7.         xref length_,prefixst_,suffixst_,printf_,putnl_
  8.         xref fload_,ffloor_,ftest_,fneg_,fmulby_
  9.         xref faddhalf_,fdivby_,cnvf2s_,cnvs2f_,itos_
  10.  
  11. ;routine rounds FP number (address in D) and adds string to BUFFER
  12.  
  13. formating  std address
  14.         jsr fload_
  15.         jsr ftest_
  16.         stb sign
  17.         if lt                   ;if number is negative
  18.            jsr fneg_
  19.         endif
  20.                                 ;  -  -  - ROUNDING OF NUMBER  -  -  -
  21.         ldd #precision          ;number x (power of 10)
  22.         jsr fmulby_
  23.         jsr faddhalf_           ;       + 0.5
  24.         jsr ffloor_             ;then is truncated
  25.         ldd #precision
  26.         jsr fdivby_             ;and devided by same number used to multiply
  27.  
  28.         tst sign
  29.         if lt                   ;'-' restored, if needed
  30.            jsr fneg_
  31.         endif
  32.  
  33.         ldd #buffer_2           ; conversion from FP to string
  34.         jsr cnvf2s_
  35.         ldy #buffer_2
  36.         clra
  37.         loop                    ;searching '.'
  38.            ldb a,y
  39.            cmpb #'.
  40.         quif eq
  41.            inca
  42.         endloop
  43.         sta position_point
  44.         leay a,y
  45.         lda digits_after_point
  46.         tst fixed_format
  47.         if eq                          ;if floating format
  48.            lda digits_after_point
  49.            loop                        ;trailing zeroes eliminated
  50.               ldb a,y
  51.               cmpb #'0
  52.               if ne
  53.                  cmpb #'.
  54.                  if eq
  55.                      clra
  56.                  else
  57.                      inca
  58.                  endif
  59.                  bra four
  60.               endif
  61.               deca
  62.           until eq
  63.         else
  64.            inca                        ;if fixed format: truncate at next char
  65.         endif
  66. four    clr a,y                        ;string  truncated
  67.  
  68.         ldb position_point
  69.         subb digits_before_point
  70. end_for_all  if gt                          ;if number is too large
  71.                ldy #buffer_2
  72.                ldb field_width
  73.                lda #'-                 ;user can use any sign he  (she) wishes
  74.                loop
  75.                  sta ,y+
  76.                  decb
  77.                until eq
  78.               clr ,y
  79.         else                           ;number is OK
  80.            sex
  81.            addd #spaces                ;leading spaces added
  82.            ldy #buffer_2
  83.               pshs y
  84.            jsr prefixst_               ;address of BUFFER_2 left on stack
  85.  
  86.            ldd #buffer_2               ;trailing  spaces added
  87.            jsr length_
  88.            subb field_width            ;maximum space allowed
  89.            sex                         ;negative sign is extended
  90.            addd #spaces                ;address is so many bytes BEFORE
  91.            jsr suffixst_               ;   SPACES
  92.              leas 2,s                  ;now P2 is removed
  93.         endif
  94.  
  95.         ldd #buffer
  96.            pshs d
  97.         ldd #buffer_2
  98.         jsr suffixst_
  99.            leas 2,s
  100.         rts
  101.  
  102. format_integer  pshs d
  103.         ldd #buffer_2
  104.         jsr itos_
  105.             leas 2,s
  106.         ldd #buffer_2
  107.         jsr length_
  108.         subb digits_before_point
  109.         bra end_for_all                 ;same end-of-routine as with FPs
  110.  
  111. position_point rmb 1
  112. sign rmb 1
  113.         fcb 0
  114.